package com.example.lss.news.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lss.news.R;
import com.example.lss.news.WebViewActivity;
import com.example.lss.news.bean.Data;
import com.example.lss.news.utils.pic.GlideApp;

import java.util.List;

public class SearchReAdapter extends RecyclerView.Adapter<SearchReAdapter.ViewHoldet> {

    private List<Data> list;

    public SearchReAdapter(List<Data> list) {
        this.list = list;
    }

    private ViewGroup context;

    @Override
    public ViewHoldet onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search, parent, false);

        return new ViewHoldet(view);
    }

    @Override
    public void onBindViewHolder(ViewHoldet holder, int position) {
        Data data = list.get(position);
        holder.setData(data);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHoldet extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView re_search_tv, re_search_baoshe, re_search_time;
        private ImageView re_search_iv1;
        private LinearLayout ll;
        Data d;

        public ViewHoldet(View itemView) {
            super(itemView);
            re_search_tv = itemView.findViewById(R.id.re_search_tv);
            re_search_baoshe = itemView.findViewById(R.id.re_search_baoshe);
            re_search_time = itemView.findViewById(R.id.re_search_time);
            ll = itemView.findViewById(R.id.ll);

            re_search_iv1 = itemView.findViewById(R.id.re_search_iv1);


            ll.setOnClickListener(this);

        }

        public void setData(Data data) {
            re_search_tv.setText(data.getTitle());
            re_search_iv1.setImageResource(R.mipmap.placehold);

            if (data.getThumbnail_pic_s() != null) {

                GlideApp.with(context).load(data.getThumbnail_pic_s()).into(re_search_iv1);
            }else if (data.getThumbnail_pic_s02() != null) {

                GlideApp.with(context).load(data.getThumbnail_pic_s02()).into(re_search_iv1);

            }
//            else {
//
//                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(800,
//                        300);
//                iv1.setLayoutParams(params);
//
//                iv2.setVisibility(View.GONE);
//            }
            else if (data.getThumbnail_pic_s03() != null) {


                GlideApp.with(context).load(data.getThumbnail_pic_s03()).into(re_search_iv1);

            }
//            else {
//                iv3.setVisibility(View.GONE);
//            }
            re_search_baoshe.setText("来自："+data.getAuthor_name());
            re_search_time.setText(data.getDate());
            d = data;


        }

        @Override
        public void onClick(View view) {
            Intent it = new Intent(context.getContext(), WebViewActivity.class);
            it.putExtra("url", d.getUrl());
            context.getContext().startActivity(it);
        }
    }
}
